<?php

namespace App\Http\Controllers\OpenShop\Feishu;


use App\Exceptions\Custom\CustomException;
use App\Exceptions\Custom\CustomHttpResponseException;
use App\Http\Controllers\BaseController;
use App\Service\OpenShop\Feishu\LoginService;
use Illuminate\Http\Request;

class LoginController extends BaseController
{

    public function login(Request $request)
    {
        // web端平台
        $webPlatform = $request->get('webPlatform', 'openShop');
        $appid       = $request->get('appId', '');
        if (!$webPlatform) {
            $webPlatform = 'openShop';
        }
        $ip = (string)$request->ip();

        //用户登录，调用rms接口
        try {
            $token = LoginService::login($request->code, $webPlatform, $appid, $ip);
        } catch (CustomHttpResponseException $exception) {
            return $this->fail($exception->getCode(), '生成登录凭证失败，请检查账户权限.' . $exception->getMessage());
        } catch (CustomException $exception) {
            return $this->fail($exception->getCode(), '登录失败, 原因 : ' . $exception->getMessage());
        }

        return $this->success($token);
    }
}
